<template>
  <div>
    <el-form ref="tsGiftOrderForm" size="small" :model="entity" :rules="rules" label-width="120px">
      <el-form-item label="订单数量" prop="shuliang">
        <el-input type="number" v-model="entity.shuliang" placeholder="需要数量" />
      </el-form-item>
      <el-form-item label="需求日期" prop="requireDate">
        <el-date-picker v-model="entity.requireDate" type="date" placeholder="需求日期" style="width: 100%;" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="订单描述" prop="descption">
        <WEditor v-bind:content.sync="entity.descption"></WEditor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from '@/utils/request'
import dayjs from "dayjs"
export default {
  props:{
    entityId : {type : Number, default: 0},
  },
  data() {
    return {
      entity: {
        id : this.entityId,
        shuliang: 1,
        requireDate:'',
        descption:'',
      },
      rules: {
        shuliang: [
          { required: true, message: '请输入订单数量', trigger: 'blur' }
        ],
        requireDate: [
          { required: true, message: '请输入需求日期', trigger: 'blur' }
        ],
        descption: [
          { required: true, message: '请输入订单描述', trigger: 'blur' },
          { min: 50, max: 20000, message: '订单描述长度50-20000个字符',  trigger: 'blur' }
        ],
      },
    }
  },
  mounted() {
    this.loadEntity();
  },
  methods: {
    loadEntity(){
      if(this.entityId > 0){
        request({ url: '/gift/order/gettsgiftorder', data: {id : this.entityId} }).then( res =>{
          this.entity = res.data
          this.entity.requireDate = dayjs(this.entity.requireDate).format('YYYY-MM-DD')
        })
      }
    },
    onSubmit(){
      this.$refs['tsGiftOrderForm'].validate((valid) => {
        if(valid){
          if(!this.entity.descption){
            this.$message.error("请填写订单描述信息");
            return
          }
          request({
            url: '/gift/order/addgiftorder',
            data: this.entity
          }).then( res =>{
            if(res.code === "200"){
              this.$message.success(res.msg)
              this.$emit('update:visible', false);  //关闭弹窗
              this.entityId > 0 ? this.$EventBus.$emit("orderedit", {}):this.$EventBus.$emit("orderadd", {})
            }else{
              this.$message.error(res.msg);
            }
          })
        }else{
          console.log('error submit!!');
          return false;
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
</style>
